package cn.itcast.nems.journal.scheduler;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.itcast.nems.studentrecord.dao.entity.JournalDropoutBatchImport;
import cn.itcast.nems.studentrecord.dao.mapper.JournalDropoutBatchImportMapper;

/**
 * <p>
 * 退学凭单批量导入 服务实现类
 * </p>
 *
 * @author 刘曙
 * @since 2024-03-24
 */
@Service
public class JournalDropoutBatchImportServiceImpl extends ServiceImpl<JournalDropoutBatchImportMapper, JournalDropoutBatchImport> implements JournalDropoutBatchImportService {

    @Override
    public List<JournalDropoutBatchImport> listPendingTask(String journalType) {
        return lambdaQuery()
                .eq(StringUtils.hasText(journalType), JournalDropoutBatchImport::getJournalType, journalType)
                .ne(JournalDropoutBatchImport::getProcessState, JournalBatchImportProcessState.FINISHED.name())
                .list();
    }

    @Override
    @Transactional
    public void fail(Integer id, String description) {
        if (StringUtils.hasText(description) && description.length() > 250) {
            description = description.substring(0, 250);
        }
        
        getBaseMapper().updateProcessResult(id, JournalBatchImportProcessState.FAILED.name(), description, null);
    }

    @Override
    @Transactional
    public void finish(Integer id, String journalId) {
        getBaseMapper().updateProcessResult(id, JournalBatchImportProcessState.FINISHED.name(), "", journalId);
    }
}
